{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入所需包\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import cross_val_score\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>magnetic_type</th>\n",
       "      <th>MagpieData maximum Number</th>\n",
       "      <th>MagpieData maximum AtomicWeight</th>\n",
       "      <th>MagpieData maximum MeltingT</th>\n",
       "      <th>MagpieData range MeltingT</th>\n",
       "      <th>MagpieData maximum NdUnfilled</th>\n",
       "      <th>MagpieData range NdUnfilled</th>\n",
       "      <th>MagpieData mean NdUnfilled</th>\n",
       "      <th>MagpieData avg_dev NdUnfilled</th>\n",
       "      <th>MagpieData maximum NfUnfilled</th>\n",
       "      <th>...</th>\n",
       "      <th>MagpieData maximum NUnfilled</th>\n",
       "      <th>MagpieData range NUnfilled</th>\n",
       "      <th>MagpieData avg_dev NUnfilled</th>\n",
       "      <th>MagpieData mean GSvolume_pa</th>\n",
       "      <th>MagpieData maximum GSmagmom</th>\n",
       "      <th>MagpieData range GSmagmom</th>\n",
       "      <th>MagpieData mean GSmagmom</th>\n",
       "      <th>MagpieData avg_dev GSmagmom</th>\n",
       "      <th>vpa</th>\n",
       "      <th>sine coulomb matrix eig 0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>138.90547</td>\n",
       "      <td>2237.0</td>\n",
       "      <td>1044.00</td>\n",
       "      <td>9.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.500000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>25.268750</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>23.976607</td>\n",
       "      <td>8720.910870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>173.05400</td>\n",
       "      <td>1728.0</td>\n",
       "      <td>1713.99</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.640000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>12.857000</td>\n",
       "      <td>0.595395</td>\n",
       "      <td>0.595395</td>\n",
       "      <td>0.119079</td>\n",
       "      <td>0.190526</td>\n",
       "      <td>8.359658</td>\n",
       "      <td>13405.612095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>140.11600</td>\n",
       "      <td>1071.0</td>\n",
       "      <td>577.00</td>\n",
       "      <td>9.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>13.0</td>\n",
       "      <td>...</td>\n",
       "      <td>22.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>8.888889</td>\n",
       "      <td>29.693333</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>24.409328</td>\n",
       "      <td>8746.562661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>23.0</td>\n",
       "      <td>50.94150</td>\n",
       "      <td>2183.0</td>\n",
       "      <td>1794.64</td>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>3.285714</td>\n",
       "      <td>3.755102</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.775510</td>\n",
       "      <td>21.362500</td>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000009</td>\n",
       "      <td>16.728137</td>\n",
       "      <td>959.014610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>92.90638</td>\n",
       "      <td>2750.0</td>\n",
       "      <td>1063.00</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>1.666667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>7.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.666667</td>\n",
       "      <td>14.589167</td>\n",
       "      <td>1.548471</td>\n",
       "      <td>1.548471</td>\n",
       "      <td>0.774236</td>\n",
       "      <td>0.774236</td>\n",
       "      <td>12.915727</td>\n",
       "      <td>4299.174667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   magnetic_type  MagpieData maximum Number  MagpieData maximum AtomicWeight  \\\n",
       "0              0                       57.0                        138.90547   \n",
       "1              0                       70.0                        173.05400   \n",
       "2              0                       58.0                        140.11600   \n",
       "3              3                       23.0                         50.94150   \n",
       "4              0                       41.0                         92.90638   \n",
       "\n",
       "   MagpieData maximum MeltingT  MagpieData range MeltingT  \\\n",
       "0                       2237.0                    1044.00   \n",
       "1                       1728.0                    1713.99   \n",
       "2                       1071.0                     577.00   \n",
       "3                       2183.0                    1794.64   \n",
       "4                       2750.0                    1063.00   \n",
       "\n",
       "   MagpieData maximum NdUnfilled  MagpieData range NdUnfilled  \\\n",
       "0                            9.0                          7.0   \n",
       "1                            2.0                          2.0   \n",
       "2                            9.0                          9.0   \n",
       "3                            8.0                          8.0   \n",
       "4                            6.0                          6.0   \n",
       "\n",
       "   MagpieData mean NdUnfilled  MagpieData avg_dev NdUnfilled  \\\n",
       "0                    5.500000                       3.500000   \n",
       "1                    0.400000                       0.640000   \n",
       "2                    3.000000                       4.000000   \n",
       "3                    3.285714                       3.755102   \n",
       "4                    3.500000                       1.666667   \n",
       "\n",
       "   MagpieData maximum NfUnfilled  ...  MagpieData maximum NUnfilled  \\\n",
       "0                            0.0  ...                           9.0   \n",
       "1                            0.0  ...                           2.0   \n",
       "2                           13.0  ...                          22.0   \n",
       "3                            0.0  ...                           8.0   \n",
       "4                            0.0  ...                           7.0   \n",
       "\n",
       "   MagpieData range NUnfilled  MagpieData avg_dev NUnfilled  \\\n",
       "0                         6.0                      3.000000   \n",
       "1                         2.0                      0.400000   \n",
       "2                        20.0                      8.888889   \n",
       "3                         6.0                      2.775510   \n",
       "4                         4.0                      1.666667   \n",
       "\n",
       "   MagpieData mean GSvolume_pa  MagpieData maximum GSmagmom  \\\n",
       "0                    25.268750                     0.000000   \n",
       "1                    12.857000                     0.595395   \n",
       "2                    29.693333                     0.000000   \n",
       "3                    21.362500                     0.000023   \n",
       "4                    14.589167                     1.548471   \n",
       "\n",
       "   MagpieData range GSmagmom  MagpieData mean GSmagmom  \\\n",
       "0                   0.000000                  0.000000   \n",
       "1                   0.595395                  0.119079   \n",
       "2                   0.000000                  0.000000   \n",
       "3                   0.000023                  0.000006   \n",
       "4                   1.548471                  0.774236   \n",
       "\n",
       "   MagpieData avg_dev GSmagmom        vpa  sine coulomb matrix eig 0  \n",
       "0                     0.000000  23.976607                8720.910870  \n",
       "1                     0.190526   8.359658               13405.612095  \n",
       "2                     0.000000  24.409328                8746.562661  \n",
       "3                     0.000009  16.728137                 959.014610  \n",
       "4                     0.774236  12.915727                4299.174667  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入材料数据\n",
    "def load_data(csv_file): \n",
    "    return pd.read_csv(csv_file, encoding = 'utf-8')\n",
    "data_form = load_data(\"rfc_data_set_after_two_step_features_selection.csv文件所在的位置\")\n",
    "data_form.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# random_state : 机器学习结果重现设置值\n",
    "data_train, data_test = train_test_split(\n",
    "    data_form, \n",
    "    test_size = 0.2, \n",
    "    shuffle = True, \n",
    "    random_state = 20210606\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取出无机磁性材料训练数据集中的特征矩阵\n",
    "X_train = data_train.drop(['magnetic_type'], axis = 1)\n",
    "# 提取出无机磁性材料训练数据集中的磁性基态\n",
    "y_train = data_train['magnetic_type']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MagpieData maximum Number</th>\n",
       "      <th>MagpieData maximum AtomicWeight</th>\n",
       "      <th>MagpieData maximum MeltingT</th>\n",
       "      <th>MagpieData range MeltingT</th>\n",
       "      <th>MagpieData maximum NdUnfilled</th>\n",
       "      <th>MagpieData range NdUnfilled</th>\n",
       "      <th>MagpieData mean NdUnfilled</th>\n",
       "      <th>MagpieData avg_dev NdUnfilled</th>\n",
       "      <th>MagpieData maximum NfUnfilled</th>\n",
       "      <th>MagpieData range NfUnfilled</th>\n",
       "      <th>MagpieData maximum NUnfilled</th>\n",
       "      <th>MagpieData range NUnfilled</th>\n",
       "      <th>MagpieData avg_dev NUnfilled</th>\n",
       "      <th>MagpieData mean GSvolume_pa</th>\n",
       "      <th>MagpieData maximum GSmagmom</th>\n",
       "      <th>MagpieData range GSmagmom</th>\n",
       "      <th>MagpieData mean GSmagmom</th>\n",
       "      <th>MagpieData avg_dev GSmagmom</th>\n",
       "      <th>vpa</th>\n",
       "      <th>sine coulomb matrix eig 0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>55818</th>\n",
       "      <td>34.0</td>\n",
       "      <td>78.9600</td>\n",
       "      <td>692.68</td>\n",
       "      <td>678.67</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.583333</td>\n",
       "      <td>10.080833</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>10.306483</td>\n",
       "      <td>3070.472668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55584</th>\n",
       "      <td>40.0</td>\n",
       "      <td>91.2240</td>\n",
       "      <td>2128.00</td>\n",
       "      <td>441.00</td>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.500000</td>\n",
       "      <td>18.701250</td>\n",
       "      <td>2.110663</td>\n",
       "      <td>2.110663</td>\n",
       "      <td>0.527666</td>\n",
       "      <td>0.791499</td>\n",
       "      <td>14.360436</td>\n",
       "      <td>4808.419945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65271</th>\n",
       "      <td>51.0</td>\n",
       "      <td>121.7600</td>\n",
       "      <td>3823.00</td>\n",
       "      <td>3769.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.875000</td>\n",
       "      <td>14.221979</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.903200</td>\n",
       "      <td>6671.718002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66870</th>\n",
       "      <td>29.0</td>\n",
       "      <td>63.5460</td>\n",
       "      <td>1519.00</td>\n",
       "      <td>1464.20</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.160714</td>\n",
       "      <td>1.782526</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.160714</td>\n",
       "      <td>9.811940</td>\n",
       "      <td>0.000310</td>\n",
       "      <td>0.000310</td>\n",
       "      <td>0.000072</td>\n",
       "      <td>0.000111</td>\n",
       "      <td>10.842968</td>\n",
       "      <td>2337.009444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2602</th>\n",
       "      <td>82.0</td>\n",
       "      <td>207.2000</td>\n",
       "      <td>2237.00</td>\n",
       "      <td>1636.39</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.640000</td>\n",
       "      <td>7.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.825000</td>\n",
       "      <td>26.468500</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>26.777186</td>\n",
       "      <td>29888.366683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43328</th>\n",
       "      <td>40.0</td>\n",
       "      <td>91.2240</td>\n",
       "      <td>2128.00</td>\n",
       "      <td>2073.20</td>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.421053</td>\n",
       "      <td>0.797784</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.196676</td>\n",
       "      <td>15.250263</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>13.960218</td>\n",
       "      <td>3580.268091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13685</th>\n",
       "      <td>30.0</td>\n",
       "      <td>65.3800</td>\n",
       "      <td>923.00</td>\n",
       "      <td>605.70</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.440000</td>\n",
       "      <td>20.976095</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>18.556524</td>\n",
       "      <td>1757.692285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69127</th>\n",
       "      <td>78.0</td>\n",
       "      <td>195.0840</td>\n",
       "      <td>2041.40</td>\n",
       "      <td>1536.32</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.592593</td>\n",
       "      <td>26.430000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>24.193280</td>\n",
       "      <td>19408.585025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78393</th>\n",
       "      <td>28.0</td>\n",
       "      <td>58.6934</td>\n",
       "      <td>3823.00</td>\n",
       "      <td>3768.20</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.173913</td>\n",
       "      <td>0.317580</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.476371</td>\n",
       "      <td>12.706977</td>\n",
       "      <td>0.595395</td>\n",
       "      <td>0.595395</td>\n",
       "      <td>0.051773</td>\n",
       "      <td>0.094543</td>\n",
       "      <td>12.873213</td>\n",
       "      <td>1652.681045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18453</th>\n",
       "      <td>70.0</td>\n",
       "      <td>173.0540</td>\n",
       "      <td>1092.00</td>\n",
       "      <td>1037.20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.691358</td>\n",
       "      <td>17.514815</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.677311</td>\n",
       "      <td>15193.889086</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>79110 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       MagpieData maximum Number  MagpieData maximum AtomicWeight  \\\n",
       "55818                       34.0                          78.9600   \n",
       "55584                       40.0                          91.2240   \n",
       "65271                       51.0                         121.7600   \n",
       "66870                       29.0                          63.5460   \n",
       "2602                        82.0                         207.2000   \n",
       "...                          ...                              ...   \n",
       "43328                       40.0                          91.2240   \n",
       "13685                       30.0                          65.3800   \n",
       "69127                       78.0                         195.0840   \n",
       "78393                       28.0                          58.6934   \n",
       "18453                       70.0                         173.0540   \n",
       "\n",
       "       MagpieData maximum MeltingT  MagpieData range MeltingT  \\\n",
       "55818                       692.68                     678.67   \n",
       "55584                      2128.00                     441.00   \n",
       "65271                      3823.00                    3769.50   \n",
       "66870                      1519.00                    1464.20   \n",
       "2602                       2237.00                    1636.39   \n",
       "...                            ...                        ...   \n",
       "43328                      2128.00                    2073.20   \n",
       "13685                       923.00                     605.70   \n",
       "69127                      2041.40                    1536.32   \n",
       "78393                      3823.00                    3768.20   \n",
       "18453                      1092.00                    1037.20   \n",
       "\n",
       "       MagpieData maximum NdUnfilled  MagpieData range NdUnfilled  \\\n",
       "55818                            0.0                          0.0   \n",
       "55584                            8.0                          8.0   \n",
       "65271                            0.0                          0.0   \n",
       "66870                            5.0                          5.0   \n",
       "2602                             2.0                          2.0   \n",
       "...                              ...                          ...   \n",
       "43328                            8.0                          8.0   \n",
       "13685                            0.0                          0.0   \n",
       "69127                            1.0                          1.0   \n",
       "78393                            2.0                          2.0   \n",
       "18453                            0.0                          0.0   \n",
       "\n",
       "       MagpieData mean NdUnfilled  MagpieData avg_dev NdUnfilled  \\\n",
       "55818                    0.000000                       0.000000   \n",
       "55584                    3.000000                       3.000000   \n",
       "65271                    0.000000                       0.000000   \n",
       "66870                    1.160714                       1.782526   \n",
       "2602                     0.400000                       0.640000   \n",
       "...                           ...                            ...   \n",
       "43328                    0.421053                       0.797784   \n",
       "13685                    0.000000                       0.000000   \n",
       "69127                    0.333333                       0.444444   \n",
       "78393                    0.173913                       0.317580   \n",
       "18453                    0.000000                       0.000000   \n",
       "\n",
       "       MagpieData maximum NfUnfilled  MagpieData range NfUnfilled  \\\n",
       "55818                            0.0                          0.0   \n",
       "55584                            0.0                          0.0   \n",
       "65271                            0.0                          0.0   \n",
       "66870                            0.0                          0.0   \n",
       "2602                             7.0                          7.0   \n",
       "...                              ...                          ...   \n",
       "43328                            0.0                          0.0   \n",
       "13685                            0.0                          0.0   \n",
       "69127                            0.0                          0.0   \n",
       "78393                            0.0                          0.0   \n",
       "18453                            0.0                          0.0   \n",
       "\n",
       "       MagpieData maximum NUnfilled  MagpieData range NUnfilled  \\\n",
       "55818                           2.0                         2.0   \n",
       "55584                           8.0                         4.0   \n",
       "65271                           4.0                         3.0   \n",
       "66870                           5.0                         4.0   \n",
       "2602                            7.0                         4.0   \n",
       "...                             ...                         ...   \n",
       "43328                           8.0                         8.0   \n",
       "13685                           3.0                         3.0   \n",
       "69127                           4.0                         2.0   \n",
       "78393                           4.0                         3.0   \n",
       "18453                           2.0                         2.0   \n",
       "\n",
       "       MagpieData avg_dev NUnfilled  MagpieData mean GSvolume_pa  \\\n",
       "55818                      0.583333                    10.080833   \n",
       "55584                      1.500000                    18.701250   \n",
       "65271                      0.875000                    14.221979   \n",
       "66870                      1.160714                     9.811940   \n",
       "2602                       0.825000                    26.468500   \n",
       "...                             ...                          ...   \n",
       "43328                      1.196676                    15.250263   \n",
       "13685                      1.440000                    20.976095   \n",
       "69127                      0.592593                    26.430000   \n",
       "78393                      0.476371                    12.706977   \n",
       "18453                      0.691358                    17.514815   \n",
       "\n",
       "       MagpieData maximum GSmagmom  MagpieData range GSmagmom  \\\n",
       "55818                     0.000000                   0.000000   \n",
       "55584                     2.110663                   2.110663   \n",
       "65271                     0.000000                   0.000000   \n",
       "66870                     0.000310                   0.000310   \n",
       "2602                      0.000000                   0.000000   \n",
       "...                            ...                        ...   \n",
       "43328                     0.000000                   0.000000   \n",
       "13685                     0.000000                   0.000000   \n",
       "69127                     0.000000                   0.000000   \n",
       "78393                     0.595395                   0.595395   \n",
       "18453                     0.000000                   0.000000   \n",
       "\n",
       "       MagpieData mean GSmagmom  MagpieData avg_dev GSmagmom        vpa  \\\n",
       "55818                  0.000000                     0.000000  10.306483   \n",
       "55584                  0.527666                     0.791499  14.360436   \n",
       "65271                  0.000000                     0.000000  14.903200   \n",
       "66870                  0.000072                     0.000111  10.842968   \n",
       "2602                   0.000000                     0.000000  26.777186   \n",
       "...                         ...                          ...        ...   \n",
       "43328                  0.000000                     0.000000  13.960218   \n",
       "13685                  0.000000                     0.000000  18.556524   \n",
       "69127                  0.000000                     0.000000  24.193280   \n",
       "78393                  0.051773                     0.094543  12.873213   \n",
       "18453                  0.000000                     0.000000  14.677311   \n",
       "\n",
       "       sine coulomb matrix eig 0  \n",
       "55818                3070.472668  \n",
       "55584                4808.419945  \n",
       "65271                6671.718002  \n",
       "66870                2337.009444  \n",
       "2602                29888.366683  \n",
       "...                          ...  \n",
       "43328                3580.268091  \n",
       "13685                1757.692285  \n",
       "69127               19408.585025  \n",
       "78393                1652.681045  \n",
       "18453               15193.889086  \n",
       "\n",
       "[79110 rows x 20 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看X_train\n",
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55818    0\n",
       "55584    2\n",
       "65271    0\n",
       "66870    2\n",
       "2602     2\n",
       "        ..\n",
       "43328    0\n",
       "13685    0\n",
       "69127    0\n",
       "78393    2\n",
       "18453    3\n",
       "Name: magnetic_type, Length: 79110, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看y_train\n",
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取出无机磁性材料测试数据集中的特征矩阵\n",
    "X_test = data_test.drop(['magnetic_type'], axis = 1)\n",
    "# 提取出无机磁性材料测试数据集中的磁性基态\n",
    "y_test = data_test['magnetic_type']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建RFC无机磁性材料磁性基态分类模型\n",
    "rfc_pipeline = Pipeline([\n",
    "    # 缺失值填充\n",
    "    ('imputer', SimpleImputer(missing_values=np.nan, strategy='mean')),\n",
    "    # RFC模型\n",
    "    ('rfc', RandomForestClassifier(n_estimators = 400, \n",
    "                                   max_features = 'log2', \n",
    "                                   min_samples_leaf = 1, \n",
    "                                   min_samples_split = 2, \n",
    "                                   n_jobs = -1)\n",
    "    )\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "         steps=[('imputer',\n",
       "                 SimpleImputer(add_indicator=False, copy=True, fill_value=None,\n",
       "                               missing_values=nan, strategy='mean',\n",
       "                               verbose=0)),\n",
       "                ('rfc',\n",
       "                 RandomForestClassifier(bootstrap=True, ccp_alpha=0.0,\n",
       "                                        class_weight=None, criterion='gini',\n",
       "                                        max_depth=None, max_features='log2',\n",
       "                                        max_leaf_nodes=None, max_samples=None,\n",
       "                                        min_impurity_decrease=0.0,\n",
       "                                        min_impurity_split=None,\n",
       "                                        min_samples_leaf=1, min_samples_split=2,\n",
       "                                        min_weight_fraction_leaf=0.0,\n",
       "                                        n_estimators=400, n_jobs=-1,\n",
       "                                        oob_score=False, random_state=None,\n",
       "                                        verbose=0, warm_start=False))],\n",
       "         verbose=False)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构建模型\n",
    "rfc_pipeline.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 磁性基态分类训练结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "磁性基态分类模型在训练集上的10折交叉验证结果：\n",
      "-----------------------------------------------------------------------\n",
      "分类准确率的每折结果：\n",
      "[0.88029326 0.87763873 0.87321451 0.87789154 0.87523701 0.87624826\n",
      " 0.87245607 0.87852357 0.87157123 0.87561623]\n",
      "分类准确率的平均值：\n",
      "0    0.875869\n",
      "dtype: float64\n",
      "-----------------------------------------------------------------------\n",
      "分类精确率的每折结果：\n",
      "[0.87953482 0.87852357 0.87308811 0.87763873 0.87523701 0.87890279\n",
      " 0.87346732 0.87751232 0.87119201 0.87725951]\n",
      "分类精确率的平均值：\n",
      "0    0.876236\n",
      "dtype: float64\n",
      "-----------------------------------------------------------------------\n",
      "分类召回率的每折结果：\n",
      "[0.87928201 0.87801795 0.87119201 0.8790292  0.87675389 0.87662748\n",
      " 0.87372014 0.87763873 0.87119201 0.87523701]\n",
      "分类召回率的平均值：\n",
      "0    0.875869\n",
      "dtype: float64\n",
      "-----------------------------------------------------------------------\n",
      "分类F1分数的每折结果：\n",
      "[0.88004045 0.87725951 0.87334092 0.87890279 0.87688029 0.87738592\n",
      " 0.87232967 0.87662748 0.87169764 0.87536342]\n",
      "分类F1分数的平均值：\n",
      "0    0.875983\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 磁性基态分类模型在训练集上的10折交叉验证结果\n",
    "print(\"磁性基态分类模型在训练集上的10折交叉验证结果：\")\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "# 分类准确率\n",
    "cross_val_score_accuracy = cross_val_score(rfc_pipeline, X_train, y_train.astype('int'), scoring = 'accuracy', cv=10)\n",
    "# 分类准确率的每折结果\n",
    "print(\"分类准确率的每折结果：\")\n",
    "print(cross_val_score_accuracy)\n",
    "# 分类准确率的平均值\n",
    "print(\"分类准确率的平均值：\")\n",
    "print(pd.DataFrame(cross_val_score_accuracy).mean())\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "\n",
    "# 分类精确率\n",
    "cross_val_score_precision = cross_val_score(rfc_pipeline, X_train, y_train.astype('int'), scoring = 'precision_micro', cv=10)\n",
    "print(\"分类精确率的每折结果：\")\n",
    "# 分类精确率的每折结果\n",
    "print(cross_val_score_precision)\n",
    "print(\"分类精确率的平均值：\")\n",
    "# 分类精确率的平均值\n",
    "print(pd.DataFrame(cross_val_score_precision).mean())\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "\n",
    "# 分类召回率\n",
    "cross_val_score_precision = cross_val_score(rfc_pipeline, X_train, y_train.astype('int'), scoring = 'recall_micro', cv=10)\n",
    "print(\"分类召回率的每折结果：\")\n",
    "# 分类召回率的每折结果\n",
    "print(cross_val_score_precision)\n",
    "print(\"分类召回率的平均值：\")\n",
    "# 分类召回率的平均值\n",
    "print(pd.DataFrame(cross_val_score_precision).mean())\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "\n",
    "# 分类F1分数\n",
    "cross_val_score_precision = cross_val_score(rfc_pipeline, X_train, y_train.astype('int'), scoring = 'f1_micro', cv=10)\n",
    "print(\"分类F1分数的每折结果：\")\n",
    "# 分类F1分数的每折结果\n",
    "print(cross_val_score_precision)\n",
    "print(\"分类F1分数的平均值：\")\n",
    "# 分类F1分数的平均值\n",
    "print(pd.DataFrame(cross_val_score_precision).mean())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 磁性基态分类测试结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "磁性基态分类模型在测试集上的10折交叉验证结果：\n",
      "-----------------------------------------------------------------------\n",
      "分类准确率的每折结果：\n",
      "[0.85793731 0.84277048 0.84782609 0.83265925 0.86097068 0.85136502\n",
      " 0.85136502 0.85844287 0.84522003 0.84673748]\n",
      "分类准确率的平均值：\n",
      "0    0.849529\n",
      "dtype: float64\n",
      "-----------------------------------------------------------------------\n",
      "分类精确率的每折结果：\n",
      "[0.85642063 0.84226491 0.84833165 0.83367037 0.85945399 0.85035389\n",
      " 0.85591507 0.85995956 0.84471421 0.8472433 ]\n",
      "分类精确率的平均值：\n",
      "0    0.849833\n",
      "dtype: float64\n",
      "-----------------------------------------------------------------------\n",
      "分类召回率的每折结果：\n",
      "[0.85945399 0.84327604 0.84934277 0.82962588 0.86248736 0.85187058\n",
      " 0.85338726 0.85642063 0.8442084  0.84926657]\n",
      "分类召回率的平均值：\n",
      "0    0.849934\n",
      "dtype: float64\n",
      "-----------------------------------------------------------------------\n",
      "分类F1分数的每折结果：\n",
      "[0.85692619 0.83973711 0.84984833 0.83215369 0.85844287 0.8554095\n",
      " 0.85035389 0.85995956 0.84572585 0.85078402]\n",
      "分类F1分数的平均值：\n",
      "0    0.849934\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 磁性基态分类模型在测试集上的10折交叉验证结果\n",
    "print(\"磁性基态分类模型在测试集上的10折交叉验证结果：\")\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "# 分类准确率\n",
    "cross_val_score_accuracy = cross_val_score(rfc_pipeline, X_test, y_test.astype('int'), scoring = 'accuracy', cv=10)\n",
    "# 分类准确率的每折结果\n",
    "print(\"分类准确率的每折结果：\")\n",
    "print(cross_val_score_accuracy)\n",
    "# 分类准确率的平均值\n",
    "print(\"分类准确率的平均值：\")\n",
    "print(pd.DataFrame(cross_val_score_accuracy).mean())\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "\n",
    "# 分类精确率\n",
    "cross_val_score_precision = cross_val_score(rfc_pipeline, X_test, y_test.astype('int'), scoring = 'precision_micro', cv=10)\n",
    "print(\"分类精确率的每折结果：\")\n",
    "# 分类精确率的每折结果\n",
    "print(cross_val_score_precision)\n",
    "print(\"分类精确率的平均值：\")\n",
    "# 分类精确率的平均值\n",
    "print(pd.DataFrame(cross_val_score_precision).mean())\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "\n",
    "# 分类召回率\n",
    "cross_val_score_precision = cross_val_score(rfc_pipeline, X_test, y_test.astype('int'), scoring = 'recall_micro', cv=10)\n",
    "print(\"分类召回率的每折结果：\")\n",
    "# 分类召回率的每折结果\n",
    "print(cross_val_score_precision)\n",
    "print(\"分类召回率的平均值：\")\n",
    "# 分类召回率的平均值\n",
    "print(pd.DataFrame(cross_val_score_precision).mean())\n",
    "print(\"-----------------------------------------------------------------------\")\n",
    "\n",
    "# 分类F1分数\n",
    "cross_val_score_precision = cross_val_score(rfc_pipeline, X_test, y_test.astype('int'), scoring = 'f1_micro', cv=10)\n",
    "print(\"分类F1分数的每折结果：\")\n",
    "# 分类F1分数的每折结果\n",
    "print(cross_val_score_precision)\n",
    "print(\"分类F1分数的平均值：\")\n",
    "# 分类F1分数的平均值\n",
    "print(pd.DataFrame(cross_val_score_precision).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
